package com.plum.admin.modules.youeorder.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.plum.admin.modules.youeorder.entity.OrderAccountDetails;
import com.plum.admin.modules.youeorder.entity.SnOrderInfo;
import com.plum.admin.modules.youeorder.entity.SysOffice;
import com.plum.admin.modules.youeorder.entity.SysOfficeRateConf;
import com.plum.admin.modules.youeorder.mapper.SnOrderInfoMapper;
import com.plum.admin.modules.youeorder.service.IOrderAccountDetailsService;
import com.plum.admin.modules.youeorder.service.ISnOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plum.admin.modules.youeorder.service.ISysOfficeRateConfService;
import com.plum.admin.modules.youeorder.service.ISysOfficeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单信息表 服务实现类
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-17
 */
@Service
@Slf4j
public class SnOrderInfoServiceImpl extends ServiceImpl<SnOrderInfoMapper, SnOrderInfo> implements ISnOrderInfoService {

    @Autowired
    private IOrderAccountDetailsService orderAccountDetailsService;
    @Autowired
    private ISysOfficeRateConfService officeRateConfService;
    @Autowired
    private ISysOfficeService officeService;
    @Override
    public void handlerBatchOrder() {
        List<SnOrderInfo> infoList = baseMapper.queryOrderList();
//        List<OrderAccountDetails> list = orderAccountDetailsService.list(Wrappers.<OrderAccountDetails>query()
//                .lambda().eq(OrderAccountDetails::getOrderStatus, 7)
//                .between(OrderAccountDetails::getOrderPayTime,"2021-07-01 00:00:00", "2021-07-31 23:59:59")
////        );
//        List<SnOrderInfo> orderList= Lists.newArrayList();
//        if(CollUtil.isNotEmpty(list)){
//            getOrderIdList(list,orderList,infoList);
//        }else{
//            orderList=infoList;
//        }
        if(infoList.size()>0){
            log.info(infoList.size()+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            insertOrder(infoList);
        }
    }
    private void insertOrder(List<SnOrderInfo> orderList){
        OrderAccountDetails accountDetails=null;
        List<OrderAccountDetails> orderInfoList=Lists.newArrayList();
        for(SnOrderInfo orderInfo:orderList){
            BigDecimal commissionPrice=BigDecimal.ZERO;//渠道抽成金额
            BigDecimal platformPrice=BigDecimal.ZERO;//平台抽成金额
            BigDecimal driverPrice=BigDecimal.ZERO;//司机抽成金额
            BigDecimal companyPrice=BigDecimal.ZERO;//运力公司抽成金额
            accountDetails=new OrderAccountDetails();
            String companyId = orderInfo.getDriver().getCompanyId();
            String joinType = orderInfo.getDriver().getJoinType();
            Integer platForm=orderInfo.getPlatform();
            BigDecimal orderFee = orderInfo.getOrderFee();
            BigDecimal otherPrice=BigDecimal.ZERO;//其他费用
            List<SysOfficeRateConf> rateList = officeRateConfService.getByOfficeId(companyId);
            if(CollUtil.isNotEmpty(rateList)){
                for (SysOfficeRateConf rateConf : rateList) {
                    if(rateConf.getType().equals(joinType)){
                        BigDecimal driver = rateConf.getDriver();
                        driverPrice=orderFee.multiply(driver).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal());
                        if(platForm.equals(1)){
                            commissionPrice=orderFee.multiply(ObjectUtil.isNull(rateConf.getDidiCompany())?BigDecimal.ZERO:rateConf.getDidiCompany()).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal());
                        }
                        if(platForm.equals(2)){
                            commissionPrice=orderFee.multiply(ObjectUtil.isNull(rateConf.getXcCompany())?BigDecimal.ZERO:rateConf.getXcCompany()).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal());
                        }
                        companyPrice=orderFee.multiply(rateConf.getTransportCompany()).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal());
                        platformPrice=orderFee.multiply(ObjectUtil.isNull(rateConf.getPlatform())?BigDecimal.ZERO:rateConf.getPlatform()).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal());
                        String othersCompany = rateConf.getOthersCompany();
                        if(StrUtil.isNotBlank(othersCompany)){
                            String[] split = othersCompany.split(",");
                            for(int i=0;i<split.length;i++){
                                String[] split1 = split[i].split("-");
                                String officeId = split1[0];
                                String rate = split1[1];
                                SysOffice office = officeService.getById(officeId);
                                if(i==0){
                                    accountDetails.setOneCompanyName(office.getName());
                                    accountDetails.setOneCompanyPrice(orderFee.multiply(new BigDecimal(rate)).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal()));
                                }
                                if(i==1){
                                    accountDetails.setTwoCompanyName(office.getName());
                                    accountDetails.setTwoCompanyPrice(orderFee.multiply(new BigDecimal(rate)).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal()));
                                }
                                if(i==2){
                                    accountDetails.setThreeCompanyName(office.getName());
                                    accountDetails.setThreeCompanyPrice(orderFee.multiply(new BigDecimal(rate)).divide(BigDecimal.valueOf(100L)).setScale(RoundingMode.HALF_DOWN.ordinal()));
                                }
                            }
                        }
                    }
                }
            }
            if(StrUtil.isNotBlank(orderInfo.getOtherPrice())){
                String[] split = orderInfo.getOtherPrice().split(",");
                for(String str:split){
                    otherPrice=otherPrice.add(new BigDecimal(str)).setScale(RoundingMode.HALF_DOWN.ordinal());
                }
            }
            accountDetails.setDriverPrice(driverPrice.add(otherPrice));
            accountDetails.setOrderPrice(orderFee);
            accountDetails.setCommissionPrice(commissionPrice);
            accountDetails.setPlatformPrice(platformPrice);
            accountDetails.setCompanyPrice(companyPrice);
            accountDetails.setOtherPrice(otherPrice);
            accountDetails.setOrderSource(orderInfo.getPlatform());
            accountDetails.setDriverId(orderInfo.getDriverid());
            accountDetails.setOrderId(orderInfo.getId());
            accountDetails.setOrderCreateTime(orderInfo.getOrderTime());
            accountDetails.setOrderPayTime(orderInfo.getPayTime());
            accountDetails.setType(1);
            accountDetails.setOrderFinishTime(orderInfo.getArrivetime());
            accountDetails.setOrderStatus(orderInfo.getDelFlag());
            accountDetails.setCreateDate(LocalDateTime.now());
            accountDetails.setCreateBy("1");
            accountDetails.setUpdateBy("1");
            accountDetails.setUpdateDate(LocalDateTime.now());
            accountDetails.setDelFlag(1);
            orderInfoList.add(accountDetails);
        }
        orderAccountDetailsService.saveBatch(orderInfoList);
    }
    private void getOrderIdList(List<OrderAccountDetails> list,List<SnOrderInfo> orderList,List<SnOrderInfo> orderInfoList){
        for(SnOrderInfo orderInfo:orderInfoList){
            if (exists(orderInfo.getId(),list)) {
                orderList.add(orderInfo);
            }
        }
    }
    private boolean exists(String orderId,List<OrderAccountDetails> list){
        boolean exists=false;
        for (OrderAccountDetails accountDetails : list) {
            if(accountDetails.getOrderId().equals(orderId)){
                continue;
            }
            exists=true;
        }
        return exists;
    }
}
